<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles_category', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('sort')->default(0);
            $table->bigInteger('parent_id')->default(0);
            $table->string('name', 120);
            $table->tinyInteger('status')->default(0);
            $table->timestamps();
            $table->index('name');
        });

        Schema::create('articles_has_category', function (Blueprint $table) {
            $table->unsignedBigInteger('category_id');
            $table->unsignedBigInteger('article_id');
            $table->index('article_id');

            $table->foreign('category_id')
                ->references('id')
                ->on('articles_category')
                ->onDelete('cascade');

            $table->primary(['category_id', 'article_id']);

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles_category');
    }
};
